// pages/home/index.js
import {
  request
} from "../../request/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signIn: "已签",
    day:0,
    week:'',
    name:''
  },


  signBtn() {
    const that=this;
    wx.getStorage({
      key: 'token',
      success(res1) {
        wx.getStorage({
          key: 'id',
          success(res2) {
            request({
              url: 'http://localhost:3088/sign/' + res2.data,
              method: 'POST'
            }).then(request => {
              that.setData({
                signIn:'已签'
              })
              that.setData({
                day:that.data.day+1
              })
              wx.showToast({
                title: request.data.message,
              })
            })

          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let day=this.getCurrentDate()
    this.setData({
      week:day
    })
    const that=this;
    wx.getStorage({
      key: 'userInfo',
      success(res){
        that.setData({
          name:res.data.nickName
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that=this; 
    wx.getStorage({
      key: 'course',
      success(res){
        that.setData({
          course:res.data
        })
      }
    })
    let day=0;
    wx.getStorage({
      key: 'id',
      success(res) {
        request({
          url: 'http://localhost:3088/sign/' + res.data
        }).then(result=>{
            that.setData({
              day:result.data.count,
            })
            if(result.data.status){
              that.setData({
                signIn:'已签'
              })
            }else{
              that.setData({
                signIn:'签到'
              }) 
            }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCurrentDate() {
    var now = new Date();
    var month = now.getMonth()+1;//得到月份
    var date = now.getDate();//得到日期
    var day = now.getDay();//得到周几
    var week;
    var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    week = arr_week[day];
    return month + "月" + date + "日" + " " + week;
} 
})